package com.lineage.server.model.npc.action;

import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.npc.L1NpcHtml;
import java.util.Iterator;
import java.util.List;
import org.w3c.dom.Element;

public class L1NpcListedAction extends L1NpcXmlAction {
  private List<L1NpcAction> _actions;
  
  public L1NpcListedAction(Element element) {
    super(element);
    this._actions = L1NpcXmlParser.listActions(element);
  }
  
  public L1NpcHtml execute(String actionName, L1PcInstance pc, L1Object obj, byte[] args) {
    L1NpcHtml result = null;
    Iterator<L1NpcAction> iterator = this._actions.iterator();
    while (iterator.hasNext()) {
      L1NpcAction action = iterator.next();
      if (!action.acceptsRequest(actionName, pc, obj))
        continue; 
      L1NpcHtml r = action.execute(actionName, pc, obj, args);
      if (r == null)
        continue; 
      result = r;
    } 
    return result;
  }
  
  public L1NpcHtml executeWithAmount(String actionName, L1PcInstance pc, L1Object obj, long amount) {
    L1NpcHtml result = null;
    Iterator<L1NpcAction> iterator = this._actions.iterator();
    while (iterator.hasNext()) {
      L1NpcAction action = iterator.next();
      if (!action.acceptsRequest(actionName, pc, obj))
        continue; 
      L1NpcHtml r = action.executeWithAmount(actionName, pc, obj, amount);
      if (r == null)
        continue; 
      result = r;
    } 
    return result;
  }
  
  public void execute(String actionName, String npcid) {
    Iterator<L1NpcAction> iterator = this._actions.iterator();
    while (iterator.hasNext()) {
      L1NpcAction action = iterator.next();
      if (!action.acceptsRequest(actionName, null, null))
        continue; 
      action.execute(actionName, npcid);
    } 
  }
}
